# -*- coding:utf-8 -*-
import threading

from flask import Flask, request, abort

app = Flask(__name__)

# lock
my_lock = threading.Lock()
# 全局的计数器
global_name_lst = list()


def check_name(name: str) -> bool:
    if name == "whw-7":
        return False
    return True


@app.route("/index", methods=["POST", "GET"])
def index2():
    if request.method == "GET":
        name = request.args.get("name")
        print("GET:>>>>>>>>>>>>>>>>>>>>>", name)
        ret = {
            "msg": f"hello GET {name}"
        }
        # Notice name校验没通过 报错
        if not check_name(name):
            abort("502", description="check name failed!")

        with my_lock:
            global_name_lst.append(name)
            print("global_name_lst: ", global_name_lst)

        return ret

    if request.method == "POST":
        name = request.json.get("name")
        print("POST:>>>>>>>>>>>>>>>>>>>>>", name)
        ret = {
            "msg": f"hello POST {name}"
        }
        return ret


if __name__ == '__main__':
    app.run(host="127.0.0.1", port=9099)
